package vip.zhenzicheng.concurrent_program.interview.alternate_output_letters;

import java.util.concurrent.locks.LockSupport;

/**
 * 交替输出字母的LockSupport实现
 *
 * @author zhenzicheng
 * @date: 2022-06-02 21:41
 */
public class Solution_LockSupport {

  static Thread t1 = null, t2 = null;

  public static void main(String[] args) {
    char[] s1 = "123456789".toCharArray();
    char[] s2 = "abcdefghi".toCharArray();

    t1 = new Thread(() -> {
      for (char c : s1) {
        System.out.print(c);
        LockSupport.unpark(t2);
        LockSupport.park();
      }
    }, "t1");

    t2 = new Thread(() -> {
      for (char c : s2) {
        LockSupport.park();
        System.out.print(c);
        LockSupport.unpark(t1);
      }
    }, "t2");

    t1.start();
    t2.start();

  }
}
